package com.jogern.baselibrary;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.AnimRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

/**
 * Fragment的基类，为抽象类
 * Created by jogern on 2016/8/15 0015.
 */

public abstract class BaseFragment extends Fragment {
    /** Fragrant的布局 */
    protected View mView;
    private BaseFragmentActivity mBaseFragmentActivity;
    protected InputMethodManager inputMethodManager;

    public BaseFragmentActivity getBaseFragmentActivity() {
        return mBaseFragmentActivity;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof BaseFragmentActivity) {
            mBaseFragmentActivity = (BaseFragmentActivity) activity;
        } else {
            throw new IllegalArgumentException("activity must extends BaseFragmentActivity");
        }
    }

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mView == null) {
            mView = onCreateView(inflater, container);
            initView(mView);
            singleInit();
        }
        ViewGroup parent = (ViewGroup) mView.getParent();
        if (parent != null) {
            parent.removeView(mView);
        }
        initOnCreateView();
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        super.onActivityCreated(savedInstanceState);
        Bundle getArguments = getArguments();
        initBundle(savedInstanceState, getArguments);
        initOther();
    }

    /** 只初始化一次,初始化控件 */
    protected void initView(View view) {}

    /** 在initView之后，只初始化一次与initView一样 */
    protected void singleInit() {}

    protected void initOnCreateView() {}

    protected void initBundle(Bundle savedInstanceState, Bundle getArguments) {}

    protected void initOther() {}

    protected void addFragment(Fragment fragment) {
        mBaseFragmentActivity.addFragment(fragment);
    }

    protected void removeTopFragment() {
        mBaseFragmentActivity.removePopFragmentOfStack();
    }

    protected void setKeyboardStateChangedListener(OnSoftKeyboardStateChangedListener
                                                           keyboardStateChangedListener) {
        mBaseFragmentActivity.setKeyboardStateChangedListener(keyboardStateChangedListener);
    }

    protected void hideSoftKeyboard() {
        if (getActivity().getWindow().getAttributes().softInputMode
                != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getActivity().getCurrentFocus() != null)
                inputMethodManager.hideSoftInputFromWindow(
                        getActivity().getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS
                );
        }
    }

    protected void setAddCustomAnimations(@AnimRes int enter, @AnimRes int exit,
                                          @AnimRes int popEnter, @AnimRes int popExit) {
        mBaseFragmentActivity.setAddCustomAnimations(enter, exit, popEnter, popExit);
    }

    protected abstract View onCreateView(LayoutInflater inflater, ViewGroup container);

    public boolean onTouchEvent(MotionEvent event) {
        return false;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }
}
